package jp.ac.waseda.cs.washi.game;

import java.awt.Image;

import jp.ac.waseda.cs.washi.game.entity.Chara;
import jp.ac.waseda.cs.washi.game.entity.GameTime;
import jp.ac.waseda.cs.washi.game.entity.Map;
import jp.ac.waseda.cs.washi.game.factory.ImageFactory;
import jp.ac.waseda.cs.washi.gameaiarena.gui.Renderer;

class ScoreRenderer {

	private final int _mapWidth, _mapHeight;
	private final int _width, _height;
	/**
	 * [0][0~9] : 数字の画像 [0][10] : 「remi」画像 [1][0~9] : 数字の画像 [1][10] : 「sora」画像
	 */
	private Image[][] _scoreImages;
	/**
	 * [0~9] : 数字の画像 [10] : 「time」画像 [11] : 「：」コロンの画像
	 */
	private Image[] _timeImages;
	private Image _scoreBoard;

	public ScoreRenderer(Renderer render, Map map) {
		_scoreImages = ImageFactory.loadScoreImages(render);
		_timeImages = ImageFactory.loadTimeImages(render);
		_mapWidth = map.getWidth() * Map.BLOCK_WIDTH;
		_mapHeight = map.getHeight() * Map.BLOCK_HEIGHT;
		_scoreBoard = ImageFactory.loadScoreBoardImage(render);
		_width = _scoreBoard.getWidth(render.getImageObserver());
		_height = _scoreBoard.getHeight(render.getImageObserver()) - 15;
	}

	public int getWidth() {
		return _width;
	}

	public int getHeight() {
		return _height;
	}

	public void drawImage(Renderer render, Chara[] charas, GameTime time) {
		drawImageScoreBoard(render);
		drawImageLabels(render);
		drawImageScore(render, charas);
		drawImageTime(render, time);
	}

	private void drawImageScoreBoard(Renderer render) {
		render.drawImage(_scoreBoard, _mapWidth, -15);
	}

	private Image digitToTimeImage(int digit) {
		return _timeImages[digit % 10];
	}

	private Image digitToScoreImage(int index, int digit) {
		return _scoreImages[index][digit % 10];
	}

	private void drawImageTime(Renderer render, GameTime time) {
		int frame = time.getCurrentTime();
		// render.drawImage(digitToTimeImage(frame), _mapWidth+120+252, 390);
		render.drawImage(digitToTimeImage(frame), _mapWidth + 120 + 255,
				_mapHeight - 130);
		render.drawImage(digitToTimeImage(frame / 10), _mapWidth + 120 + 170,
				_mapHeight - 130);
		render.drawImage(digitToTimeImage(frame / 100), _mapWidth + 120 + 85,
				_mapHeight - 130);
		render.drawImage(digitToTimeImage(frame / 1000), _mapWidth + 120,
				_mapHeight - 130);
	}

	private void drawImageScore(Renderer render, Chara[] charas) {
		// doremiのスコア描画
		{
			int score = charas[0].getScore();
			render.drawImage(digitToScoreImage(0, score), _mapWidth + 20 + 168,
					130);
			render.drawImage(digitToScoreImage(0, score / 10),
					_mapWidth + 20 + 84, 130);
			render.drawImage(digitToScoreImage(0, score / 100), _mapWidth + 20,
					130);
		}
		// ブレイクボーナスを表示する
		// {
		// int score = charas[0].getBreaks();
		// render.drawImage(digitToScoreImage(0, score), _mapWidth+20+168, 300);
		// render.drawImage(digitToScoreImage(0, score / 10), _mapWidth+20+84,
		// 300);
		// render.drawImage(digitToScoreImage(0, score / 100), _mapWidth+20,
		// 300);
		// }
		// soraのスコア描画
		{
			int score = charas[1].getScore();
			render.drawImage(digitToScoreImage(1, score),
					_mapWidth + 312 + 84 + 84, 130);
			render.drawImage(digitToScoreImage(1, score / 10),
					_mapWidth + 312 + 84, 130);
			render.drawImage(digitToScoreImage(1, score / 100),
					_mapWidth + 312, 130);
		}
		// {
		// int score = charas[1].getBreaks();
		// render.drawImage(digitToScoreImage(1, score), _mapWidth+312+84+84,
		// 300);
		// render.drawImage(digitToScoreImage(1, score / 10), _mapWidth+312+84,
		// 300);
		// render.drawImage(digitToScoreImage(1, score / 100), _mapWidth+312,
		// 300);
		// }
	}

	private void drawImageLabels(Renderer render) {
		// ラベル表示
		render.drawImage(_scoreImages[0][10], _mapWidth + 10, 20);
		render.drawImage(_scoreImages[1][10], _mapWidth + 315, 20);
		render.drawImage(_timeImages[10], _mapWidth + 160, _mapHeight - 220);
	}
}
